<template>
	<div class="mod-role">
		<!-- 多条件查询数据表单 -->
		<el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
			<el-form-item>
				<el-select v-model="dataForm.sid" placeholder="所属院校" @change="getClassBySchoolId">
					<el-option label="--所属院校--" value=""></el-option>
					<el-option v-for="s in schoolList" :key="s.id" :label="s.name" :value="s.id"></el-option>
				</el-select>
			</el-form-item>

      <el-form-item>
      	<el-select v-model="dataForm.cid" placeholder="所属班级">
      		<el-option label="--所属班级--" value=""></el-option>
      		<el-option v-for="s in classList" :key="s.id" :label="s.name" :value="s.id"></el-option>
      	</el-select>
      </el-form-item>

			<el-form-item>
				<el-input v-model="dataForm.name" placeholder="学生名字" clearable></el-input>
			</el-form-item>

			<el-form-item>
				<el-button @click="getDataList()">查询</el-button>
				<el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
				<el-button type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除
				</el-button>
			</el-form-item>
		</el-form>
		<el-table :data="dataList" border v-loading="dataListLoading" @selection-change="selectionChangeHandle"
			>
			<el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
			<el-table-column prop="id" header-align="center" align="center" width="80" label="ID"></el-table-column>
			<el-table-column prop="name" header-align="center" align="center" label="姓名"></el-table-column>
      <el-table-column prop="sex" header-align="center" align="center" label="性别"></el-table-column>
      <el-table-column prop="phone" header-align="center" align="center" label="手机号码"></el-table-column>
      <el-table-column prop="cardId" header-align="center" align="center" label="身份证号码"></el-table-column>
			<el-table-column prop="schoolName" header-align="center" align="center" label="学校名称"></el-table-column>
			<el-table-column prop="major" header-align="center" align="center" label="专业"></el-table-column>
      <el-table-column prop="className" header-align="center" align="center" label="班级名称"></el-table-column>
      <el-table-column prop="status" header-align="center" align="center" label="学生状态"></el-table-column>
      <el-table-column prop="startDate" header-align="center" align="center" label="入校时间"></el-table-column>
      <el-table-column prop="courseName" header-align="center" align="center" label="当前课程"></el-table-column>
			<el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
				<template slot-scope="scope">
					<el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id,scope.row)">修改</el-button>
					<el-button type="text" size="small" @click="deleteHandle(scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
			:page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
			layout="total, sizes, prev, pager, next, jumper">
		</el-pagination>
		<!-- 弹窗, 新增 / 修改 -->
		<add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
	</div>
</template>

<script>
	import AddOrUpdate from './student-add-or-update'
	export default {
		data() {
			return {
				dataForm: {
					sid: "",
          cid: "",
					name:""
				},
				schoolList: [],
        classList:[],//所有班级
				dataList: [],
				pageIndex: 1,
				pageSize: 10,
				totalPage: 0,
				dataListLoading: false,
				dataListSelections: [],
				addOrUpdateVisible: false
			}
		},
		components: {
			AddOrUpdate
		},
		activated() {
			this.getDataList();
			this.getSchoolList();
		},
		methods: {
      getClassBySchoolId:function(schoolId){
        this.$http({
        	url: this.$http.adornUrl('/sys/classes/getClassesBySchoolId'),
        	method: 'get',
        	params: this.$http.adornParams({schoolId:schoolId}, false),
        }).then(({
        	data
        }) => {
        	if (data && data.code === 0) {
            console.log(data);
        		this.classList = data.data;
        	} else {
        		this.classList = []
        	}
        	this.dataListLoading = false
        })
      },
			// 获取数据列表
			getDataList() {
				this.dataListLoading = true
				this.$http({
					url: this.$http.adornUrl('/sys/student/getAllStudents'),
					method: 'get',
					params: this.$http.adornParams({
							'page': this.pageIndex,
							'limit': this.pageSize,
							'sid': this.dataForm.sid,
              'cid': this.dataForm.cid,
							'name': this.dataForm.name,
						},
						false
					),
				}).then(({
					data
				}) => {
					if (data && data.code === 0) {
						console.log(data);
						console.log(data.data);
						this.dataList = data.data.list;
						this.totalPage = data.data.totalCount;
					} else {
						this.dataList = []
						this.totalPage = 0
					}
					this.dataListLoading = false
				})
			},
			getSchoolList() {
				this.$http({
					url: this.$http.adornUrl('/sys/sysSchool/getSchoolAll'),
					method: 'get',
					params: this.$http.adornParams({}, false),
				}).then(({
					data
				}) => {
					if (data && data.code === 0) {
						console.log(data);
						console.log(data.data);
						this.schoolList = data.data;
					} else {
						this.schoolList = []
					}
					this.dataListLoading = false
				})
			},
			// 每页数
			sizeChangeHandle(val) {
				this.pageSize = val
				this.pageIndex = 1
				this.getDataList()
			},
			// 当前页
			currentChangeHandle(val) {
				this.pageIndex = val
				this.getDataList()
			},
			// 多选
			selectionChangeHandle(val) {
				this.dataListSelections = val
			},
			// 新增 / 修改
			addOrUpdateHandle(id,row) {
				console.log(id,row);
				this.addOrUpdateVisible = true
				let schoolList = this.schoolList;
				this.$nextTick(() => {
					this.$refs.addOrUpdate.init(id,row,schoolList)
				})
			},
			// 删除
			deleteHandle(rowData) {
				let id = rowData.cid;
				var ids = id ? [id] : this.dataListSelections.map(item => {
					return item.id
				})
				this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					this.$http({
						url: this.$http.adornUrl('/sys/student/delStudents'),
						method: 'post',
						data: this.$http.adornData(ids, false)
					}).then(({
						data
					}) => {
						if (data && data.code === 0) {
							this.$message({
								message: '操作成功',
								type: 'success',
								duration: 1500,
								onClose: () => {
									this.getDataList()
								}
							})
						} else {
							this.$message.error(data.msg)
						}
					})
				}).catch(() => {})
			}
		}
	}
</script>
